﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class MVLABEL : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        MVLABELData m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public INTEXDBC INTEXDBC;
        private DataRow dwUI;
        DataTable tableUI;                
        #endregion
        public MVLABEL()
        {
            InitializeComponent();
            this.Name = "打印标签";
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new MVLABELData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("ARTICLE", typeof(string));
            tableUI.Columns.Add("SUPART", typeof(string));
        }

        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow(); 
                InitDataBinding();
                txtARTTYPE.Focus();
                /*
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT Distinct B.LSNAME1,A.ABPAUFKRS ,A.ABPAUFLFD,A.ABPAUFPOS,A.ABPART,A.ABPVAR, "
                    + "　A.ABPFARBE,A.ABPSOLLMENGE1/100 AS WEI, A.ABPSTARTDAT,A.ABPENDDAT,'' AS REMARK, "
                    + " C.ABADIAUFKRS||'-'||TO_NUMBER(C.ABADIAUFLFD)||'-'||TO_NUMBER(C.ABADIAUFPOS) AS DIORDER "
                    + " FROM ABPOS A,LIEFDAT B,ABAUFM C "
                    + " WHERE A.ABPMANDANT = B.LSMANDANT AND A.ABPLIEFNR = B.LSLIEFNR "
                    + " AND A.ABPMANDANT = C.ABAMANDANT AND A.ABPAUFKRS = C.ABAAUFKRS "
                    + " AND A.ABPAUFLFD = C.ABAAUFLFD AND A.ABPAUFPOS = C.ABAAUFPOS "
                    + " AND A.ABPAUFART='PR'  AND A.ABPERLKZ <>'J' AND C.ABAAPOS='0001' "
                 ;
                */
                /*DataSet setResult = new DataSet();
                setResult = m_Data.LoadData(); 
                setUI.Merge(setResult);  
                this.DataContext = setUI;*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    setUI.Clear();
                    setUI.AcceptChanges();
                    string ARTICLE = txtART.Text.Trim();
                    string SUPART = txtSUPART.Text.Trim();

                    dwUI["ARTICLE"] = ARTICLE;
                    dwUI["SUPART"] = SUPART;
                    setResult = m_Data.BuildMVLABELData(dwUI);
                    setUI.Merge(setResult);
                    MasterDg.DataContext = setUI.Tables["WASTAMM"];
                }
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnQueryMaxNO_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal;
            string ARTTYPE = txtARTTYPE.Text.ToString().Trim();
            
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "select substr(vaaart,1,2) as arttype, "
                    + " max(to_number(substr(vaaart,3,length(trim(vaaart))-2))) as maxnum "
                    + " from vaart where substr(vaaart,1,2) = '"+ARTTYPE+ "' " 
                    + " group by substr(vaaart,1,2) "
                ;

                setResult = m_Data.Connect2Proxy(orlReadCommand);
                if (setResult.Tables.Count > 0)
                {
                    tbLocal = setResult.Tables[0];                    
                    foreach (DataRow dw in tbLocal.Rows)
                    {                        
                        txtMaxNO.Text = dw["maxnum"].ToString().Trim();
                    }
                }
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string art = txtARTICLE.Text.ToString().Trim();
            string color = txtCOLOR.Text.ToString().Trim();
            DataSet Result;
            Result=m_Data.GetLableContent(art,color);
            m_DataX = new MVLABELData(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            MVLABELReport m_rpt = new MVLABELReport(dicParamater);
            m_rpt.dsSource = Result;
            m_rpt.exAgent = m_DataX.exAgent;
            int copies = 1; //System.Convert.ToInt16(txtCopies.Text.ToString());
            bool IsChoosePrinter = false;
            if (ChkPrinter.IsChecked==true)
            {
                IsChoosePrinter = true;
            }
            m_rpt.GetReport("EXCEL", art, "DirectPrint",copies,IsChoosePrinter); 

        }

        private void btnEXCEL_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new MVLABELData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            MVLABELReport m_rpt = new MVLABELReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL");
        }

        private void btnPrintExcel_Click(object sender, RoutedEventArgs e)
        {
            string art = txtARTICLE.Text.ToString().Trim();
            string color = txtCOLOR.Text.ToString().Trim();
            DataSet Result;
            Result = m_Data.GetLableContent(art,color);
            m_DataX = new MVLABELData(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            MVLABELReport m_rpt = new MVLABELReport(dicParamater);
            m_rpt.dsSource = Result;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.GetReport("EXCEL", art, "OpenExcel"); 

        }

        private void txtARTTYPE_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            txtMaxNO.Text = "";
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQueryMaxNO_Click(null,null);

        }

        private void txtARTICLE_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnPrint_Click(null,null);
        }

        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null,null);

        }

    }
}
